package br.usp.icmc.biomac.shared;

import java.util.LinkedList;
import java.util.List;

/**
 * Keeps a history of messages to the logger. By default it keeps the last 100
 * messages, but this can be configured.
 *
 * @author amsnyder
 */
public class MessageHistory {

    final int limit;
    final LinkedList<Message> messageList;

    public MessageHistory(int size){
        limit=size;
        messageList = new LinkedList<Message>();
    }

    /**
     * Add a message to the history. If the size of the list is at the limit
     * remove a message from the start.
     * @param message
     */
    public void add(Message message){
        messageList.addLast(message);
        if(messageList.size()>=limit-1){
            messageList.removeFirst();
        }
    }

    /**
     * Get latest set of messages.
     * @return
     */
    public List<Message> getMessages(){
        return messageList;
    }

}
